package com.yu8home.opcua.test;

import com.yu8home.opcua.OpcUAUtil;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription;
import org.eclipse.milo.opcua.sdk.client.subscriptions.OpcUaSubscriptionManager;
import org.eclipse.milo.opcua.stack.core.AttributeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
import org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode;
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
import org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest;
import org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters;
import org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class SingleSub {

    public static void main(String[] args) throws Exception {
        OpcUAUtil.getConnect();
        createSubscribe();
        System.in.read();
    }


    public static void createSubscribe() throws Exception {
        OpcUaSubscriptionManager subscriptionManager = OpcUAUtil.opcUaClient.getSubscriptionManager();
        UaSubscription uaSubscription = subscriptionManager.createSubscription(1000.0).get();

        List<MonitoredItemCreateRequest> requests = new ArrayList<>();
        for (int i = 0; i < Const.subscribes.size(); i++) {
            String node = Const.subscribes.get(i);
            NodeId nodeId = new NodeId(OpcUAUtil.namespaceIndex, node);

            ReadValueId readValueId = new ReadValueId(
                    nodeId,
                    AttributeId.Value.uid(),
                    null,
                    QualifiedName.NULL_VALUE);

            MonitoringParameters monitoringParameters = new MonitoringParameters(
                    uaSubscription.nextClientHandle(),     // UInteger.valueOf(++i), // 监控项ID
                    1000.0,                                // 采样间隔（ms）
                    null,                                  // ExtensionObject.encode(Conn.opcUaClient.getStaticSerializationContext(), Const.eventFilter),
                    UInteger.valueOf(10),
                    true);

            MonitoredItemCreateRequest monitoredItemCreateRequest = new MonitoredItemCreateRequest(
                    readValueId,
                    MonitoringMode.Reporting,
                    monitoringParameters);

            requests.add(monitoredItemCreateRequest);
        }

        uaSubscription.createMonitoredItems(
                        TimestampsToReturn.Neither,
                        requests,
                        (item, id) -> {
                            item.setValueConsumer((k, v) -> {
                                NodeId _nodeId = k.getReadValueId().getNodeId();
                                log.info("订阅：{} = {}", _nodeId.getIdentifier(), v.getValue().getValue());
                            });
                        })
                .get();
    }

}
